package com.haulmont.asu.web.ui.theme_booking_command;

import com.haulmont.asu.entity.DiplomaProject;
import com.haulmont.asu.entity.ProjectThemeBooking;
import com.haulmont.asu.entity.ThemeBookingCommand;
import com.haulmont.cuba.core.global.Metadata;
import com.haulmont.cuba.gui.WindowManager;
import com.haulmont.cuba.gui.components.*;
import com.haulmont.cuba.gui.components.actions.CreateAction;
import com.haulmont.cuba.gui.data.CollectionDatasource;
import com.haulmont.cuba.gui.data.Datasource;
import com.haulmont.cuba.gui.data.impl.DsListenerAdapter;

import javax.annotation.Nullable;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Author: Sergey42
 * Date: 17.11.13 21:13
 */
public class ThemeBookingCommandEditor extends AbstractEditor<ThemeBookingCommand> {

    @Inject
    protected Metadata metadata;
    @Inject
    protected Table bookingTable;
    @Inject
    protected LookupField cathedraLF;
    @Inject
    protected CollectionDatasource<ThemeBookingCommand, UUID> parentCommandDs;

    @Override
    public void init(Map<String, Object> params) {
        Action createAction = bookingTable.getAction("create");
        bookingTable.removeAction(createAction);

        bookingTable.addAction(new CreateAction(bookingTable, WindowManager.OpenType.DIALOG));
    }

    @Override
    protected void postInit() {
        parentCommandDs.addListener(new DsListenerAdapter<ThemeBookingCommand>() {
            @Override
            public void itemChanged(Datasource<ThemeBookingCommand> ds,
                                    @Nullable ThemeBookingCommand prevItem,
                                    final @Nullable ThemeBookingCommand item) {
                showOptionDialog(
                        getMessage("fillTitle"),
                        getMessage("fillText"),
                        MessageType.CONFIRMATION,
                        new Action[]{
                                new DialogAction(DialogAction.Type.OK) {
                                    @Override
                                    public void actionPerform(Component component) {
                                        ThemeBookingCommand tbc = getItem();
                                        cathedraLF.setEditable(true);
                                        if (item != null) {
                                            tbc.setCathedra(item.getCathedra());
                                            tbc.setCommandText(item.getCommandText());
                                            List<ProjectThemeBooking> bookings = item.getProjectThemeBookings();
                                            if (bookings != null) {
                                                List<ProjectThemeBooking> thisBookings = new ArrayList<>();
                                                tbc.setProjectThemeBookings(thisBookings);

                                                for (ProjectThemeBooking booking : bookings) {
                                                    DiplomaProject diplomaProject = booking.getDiplomaProject();
                                                    ProjectThemeBooking copy = metadata.create(ProjectThemeBooking.class);
                                                    copy.setDiplomaProject(diplomaProject);
                                                    copy.setThemeBookingCommand(getItem());
                                                    thisBookings.add(copy);
                                                }
                                            }
                                        } else {
                                            tbc.setCathedra(null);
                                            tbc.setProjectThemeBookings(new ArrayList<ProjectThemeBooking>());
                                        }
                                        cathedraLF.setEditable(false);
                                    }
                                }

                        });
            }
        });
    }
}
